﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class EnemyBase : MonoBehaviour, Damageable, ItemTriggerReceiver
{
    protected enum EnumState
    {
        None = 0,
        Idle,
        Walk,
        Jump,
        Die,
        Fall,
        Working,
        Prepare,
        Hidden,
        StandingUp,
        StandUp,

    }

    [Header("基础配置")]
    public float health = 1;
    protected GameObject boomPrefab;

    public abstract float Damage(GameObject source, float value);

    [Tooltip("状态")]
    protected EnumState state = EnumState.None;
    [Tooltip("动画")]
    protected Animator animator;


    protected bool appearInViewport = false;
    protected bool running;
    protected Rigidbody2D rig;
    protected CameraLevel cameraLevel;
    protected Camera3DLevel camera3DLevel;
    protected SpriteRenderer mainSR;

    protected LayerMask groundLayer;

    protected new Collider2D collider2D;


    protected virtual void Init(){
        animator = GetComponent<Animator>();
        rig=GetComponent<Rigidbody2D>();
        mainSR=GetComponent<SpriteRenderer>();
        cameraLevel=Camera.main.GetComponent<CameraLevel>();
        camera3DLevel=Camera.main.GetComponent<Camera3DLevel>();
        groundLayer=1<<LayerMask.NameToLayer("跳台");
        collider2D=GetComponent<Collider2D>();
    }
    public virtual void OnCollisionEnter2D(Collision2D collision)
    {
    }

    public virtual void OnCollisionExit2D(Collision2D collision)
    {
    }

    public virtual void OnCollisionStay2D(Collision2D collision)
    {
    }

    public virtual void OnTriggerEnter2D(Collider2D collision)
    {
        if (Utility.IsLayer(collision.gameObject, "Player"))
        {
            Run();
        }
    }

    public virtual void OnTriggerExit2D(Collider2D collision)
    {
    }

    public virtual void OnTriggerStay2D(Collider2D collision)
    {
    }

    public virtual void Run()
    {
        if (!running)
        {
            running = true;
        }
    }

    public virtual void Stop()
    {
        if (running)
        {
            running = false;
        }
    }


    protected virtual bool InViewPort(float expandSize = 0.0f)
    {
        Bounds bounds = GetComponent<Renderer>().bounds;

        Vector2 leftUpPoint = new Vector2(bounds.min.x, bounds.max.y);
        Vector2 rightUpPoint = new Vector2(bounds.max.x, bounds.max.y);
        Vector2 leftDownPoint = new Vector2(bounds.min.x, bounds.min.y);
        Vector2 rightDownPoint = new Vector2(bounds.max.x, bounds.min.y);


        return Utility.InCameraViewport(Camera.main, leftUpPoint, expandSize)
            || Utility.InCameraViewport(Camera.main, rightUpPoint, expandSize)
            || Utility.InCameraViewport(Camera.main, leftDownPoint, expandSize)
            || Utility.InCameraViewport(Camera.main, rightDownPoint, expandSize)
        ;
    }

    protected bool AllInViewPort(float expandSize = 0.0f)
    {
        Bounds bounds = GetComponent<Renderer>().bounds;

        Vector2 leftUpPoint = new Vector2(bounds.min.x, bounds.max.y);
        Vector2 rightUpPoint = new Vector2(bounds.max.x, bounds.max.y);
        Vector2 leftDownPoint = new Vector2(bounds.min.x, bounds.min.y);
        Vector2 rightDownPoint = new Vector2(bounds.max.x, bounds.min.y);


        return Utility.InCameraViewport(Camera.main, leftUpPoint, expandSize)
            && Utility.InCameraViewport(Camera.main, rightUpPoint, expandSize)
            && Utility.InCameraViewport(Camera.main, leftDownPoint, expandSize)
            && Utility.InCameraViewport(Camera.main, rightDownPoint, expandSize)
        ;
    }

    //判断对象是否在屏幕内
    protected virtual void UpdateAppear()
    {
        if (InViewPort())
        {
            //只有在屏幕出现过，然后离开视窗才会销毁
            appearInViewport = true;
        }
        else if (appearInViewport)
        {
            Destroy(gameObject);
        }
    }

    protected virtual void Boom(Vector2 position, bool destroy = true)
    {
        if (boomPrefab != null)
        {
            Instantiate(boomPrefab, position, Quaternion.identity);
        }
        if (destroy)
        {
            Destroy(gameObject);
        }

    }
    protected virtual void Boom(bool destroy = true)
    {
        Boom(transform.position, destroy);
    }

    public abstract void Die();
    public virtual void DieAndDestroy(){
        Die();
    }

    public abstract bool IsDie();

}
